public with sharing class QuoteController {
    public List<ProductBean> pbList { get; set; }
    public List<QuoteProductBean> qpbList { get; set; }
    public Boolean Approve{get;set;}
    public Quote q { get; set; }
    public QuoteProduct__c qp {get;set;}//

    public String name { get; set; }
    public String brand { get; set; }
    public String serial { get; set; }
    public String category0 { get; set; }
    public String category1 { get; set; }
    public String spec {get;set;}///规格 
    public String model {get;set;}///型号
    public String productType {get;set;}//产品类型

    public List<SelectOption> cate0Opts { get; private set; }
    public List<SelectOption> cate1Opts { get; private set; }
    public List<SelectOption> corpcodeOpts { get; private set; }
    public List<SelectOption> vsalestrucodeOpts { get; private set; }

    public String qId { get; private set; }  // 报价Id
    public String oppId { get; private set; }  // 业务机会Id
    public String type { get; private set; }
    public Boolean cantSeeLowFlg { get; private set; }
    public Boolean isShijiQuote { get; private set;}//是否为试剂报价
    public Boolean ishQuote { get; private set;}//是否为合同评审表关联报价
    public Boolean isYiqiQuote { get; private set;}//是否为合同评审表关联报价
    public String  backId {get;set;}//返回Id 
    public Integer tableHeight {get;set;}//选择产品的表格的高度

    public List<QuoteProductBean> inList {get;set;}//意向产品
    public Boolean isNewQuote  ;//是否是新的报价
    public Boolean isSuccess {get;private set;}///是否保存成功
    private User u;
    private Opportunity opp;
    private String purpose;
    public Boolean isCouldFindOverseaProduct;




    public QuoteController(ApexPages.StandardController stdController) {
        Approve = false;
        pbList = new List<ProductBean>();
        qpbList = new List<QuoteProductBean>();
        inList = new List<QuoteProductBean>();
        isSuccess  = false;
        qId = stdController.getId();
        qId = qId == null ? System.currentPageReference().getParameters().get('qid'): qId;
        isNewQuote = qId == null ;
        oppId = System.currentPageReference().getParameters().get('oppid');

        backId = qId == null ? oppId : qId;
        type = System.currentPageReference().getParameters().get('t');
        cantSeeLowFlg = false;
        isShijiQuote = type == 's';
		isYiqiQuote   = type == 'y';      
        // picklist
        cate0Opts = new List<SelectOption>();
        cate0Opts.add(new SelectOption('', System.Label.Option_None));
        for (String str : System.Label.Product_Category0.split(',', -1)) {
            cate0Opts.add(new SelectOption(str, str));
        }

        cate1Opts = new List<SelectOption>();
        cate1Opts.add(new SelectOption('', System.Label.Option_None));
        for (String str : System.Label.Product_Category1.split(',', -1)) {
            cate1Opts.add(new SelectOption(str, str));
        }

        purpose = type == 'y' ? '仪器报价用' : '试剂报价用';
        u = [select Profile.Name ,SubCompany__c from User where Id = :UserInfo.getUserId()];
        isCouldFindOverseaProduct = u.Profile.Name.contains('海外') ? true : false;

    }

    public void init() {
       
        
             // 报价信息
            q = new Quote();
            qp = new QuoteProduct__c();

            if (String.isBlank(qId) == false) {///编辑报价
                Schema.DescribeSobjectResult m = Quote.sObjectType.getDescribe();
                Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();

                String sql = 'select ';
                String fields = '';
                for (String field : fieldMap.keySet()) {
                    if (fields.length() > 0) {
                        fields += ', '; 
                    }
                    fields += field;
                }
                sql += fields;
                sql += ' from Quote where Id = \'' + qId + '\'';
                q = Database.query(sql);

            } else {///新建报价

                if (String.isBlank(oppId) == false) {
                    Opportunity tmp = [select Account.Name from Opportunity where Id = :oppId];
                    // Date d = Date.today();
                    q.Name = tmp.Account.Name + '报价-' + Utils.date2string(System.now(), 'YYYYMMDDHHMMSS'); 
                } else {
                    q.Name = '*';
                }
                q.Type__c = type == 'y' ? '仪器报价' : '试剂报价';
                q.RecordTypeId = type=='s'? Utils.getRecordTypeIdByDevNameAndObjName('TerminalCustomerReagentQuote','Quote'):Utils.getRecordTypeIdByDevNameAndObjName('TerminalCustomerInstrumentQuote','Quote');
            }

            if(q.Status=='申请中' || q.Status=='已批准')
            {
                Approve = true;
                ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'此报价已审批，禁止修改'));

            }


            // 获取报价产品
            if (String.isBlank(qId) == false) {///编辑
               getQuoteProduct(qId,false);
            } else {//新建
                 List<Quote> lastQList = [select Id from Quote where OpportunityId =:oppId and Status ='已批准' and Type__c=:q.Type__c and isActive__c = true Order by Createddate desc limit 1 ];
                 if (lastQList.size() != 0 ){///带入上次审批通过的报价产品
                    getQuoteProduct(lastQList[0].Id,true);
                }else{//如果没有则带入意向产品

                }
                

            }
            if(oppId == null ) oppId = q.OpportunityId;
            getIntentProduct();


            // 新建
            if (String.isBlank(oppId) == false) {
                opp = [select Id, Owner.UserDepartment__c, Owner.Profile.Name, RecordType.Name from Opportunity where Id = :oppId];
            }
            // 编辑
            else if (String.isBlank(qId) == false) {
                opp = [select Id, Owner.UserDepartment__c, Owner.Profile.Name, RecordType.Name from Opportunity where Id = :q.OpportunityId];
            }

            tableHeight = qpbList.size() < 10 ? 180 + qpbList.size()*32 : 400;
        
       
    }
    public void getQuoteProduct(String quoteId ,Boolean isLastquote){
         Schema.DescribeSobjectResult m = QuoteProduct__c.sObjectType.getDescribe();
                Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();

                String sql = 'select ';
                String fields = '';
                for (String field : fieldMap.keySet()) {
                    if (fields.length() > 0) {
                        fields += ', '; 
                    }
                    fields += field;
                }
                sql += fields;
                sql += ' , Product__r.NCCode__c ,Product__r.swjhdj__c from QuoteProduct__c where Quote__c = \'' + quoteId+ '\' and Quote__r.Type__c = \''+q.Type__c+'\'';
                
                List<QuoteProduct__c> temp = Database.query(sql);
                Set<Id>  pids= new Set<Id>();
                for (QuoteProduct__c qp : temp) {
                    pids.add(qp.Product__c);
                }
                Map<Id,Price__c>priceMap = getPriceMap(pids);
                for (QuoteProduct__c qp : temp) {
                    qpbList.add(new QuoteProductBean(qp,isLastquote,priceMap.get(qp.Product__c)));
                }
                System.debug(qpbList);
                System.debug(isLastquote ? '从上次带入':'这次');

    }
    public void getIntentProduct(){
        inList.clear();
         Schema.DescribeSobjectResult m = IntentProduct__c.sObjectType.getDescribe();
                Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();

                String sql = 'select ';
                String fields = '';
                for (String field : fieldMap.keySet()) {
                    if (fields.length() > 0) {
                        fields += ', '; 
                    }
                    fields += field;
                }
                sql += fields;
                sql += ' , Product__r.NCCode__c,Product__r.swjhdj__c  from IntentProduct__c where Opportunity__c = \'' + oppId + '\'  ';
                if(isShijiQuote == true) sql += ' and Product__r.Category0__c != \'仪器\' ';
                if(isYiqiQuote == true) sql += ' and Product__r.Category0__c not in (\'试剂\',\'耗材\',\'校准\',\'质控\',\'耗品\',\'试纸\',\'第三方产品\',\'配件\') ';
                List<IntentProduct__c> temp =  Database.query(sql);
                Set<Id>  pids= new Set<Id>();
                for (IntentProduct__c ip :temp) {
                    pids.add(ip.Product__c);
                }
                Map<Id,Price__c>priceMap = getPriceMap(pids);
                for (IntentProduct__c ip :temp) {
                    inList.add(new QuoteProductBean(ip,priceMap.get(ip.Product__c)));
                }
                System.debug(sql);
                System.debug(qpbList);
                System.debug('从意向产品带入');

    }
   
    public void save() {
        Savepoint sp = Database.setSavepoint();
        try {

            
            // 必须有产品才能保存
            if (qpbList.size() == 0) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '请选择产品'));
                return ;
            }

            // 报价upsert
            if (String.isBlank(qId) == true && q.OpportunityId == null ) {
                q.OpportunityId = oppId;
            }
           

            

            // 获取价格信息
            List<Id> pIdList = new List<Id>();
            for (QuoteProductBean qpb : qpbList) {
                pIdList.add(qpb.productId);
            }
            Map<Id, Price__c> priceMap = new Map<Id, Price__c>();
            for (Price__c pr : [select Id, Product__c,Product__r.bggsjhdj__c, MarketPrice__c, HospitalManagerPrice__c, HospitalBottomPrice__c, CompanyBottomPrice__c,
                AreaDealerPrice__c, InvoiceDealerPrice__c, RepairCost__c, InstCost__c ,LastMonthAveragePrice__c from Price__c where Product__c in : pidList and Company__c = :u.SubCompany__c]) {
                System.debug(pr);
                priceMap.put(pr.Product__c, pr);
            }
                     /// 日期处理
                Integer m = Date.today().month(); //当前月份
                Integer y = Date.today().year();  //当前年份 
               ///取上月
                 if(m == 1){
                m = 12 ;
                    y -= 1;
                }else{
                m -= 1;

                }

                String  month = String.valueOf(m) + '月';
                String  year= String.valueOf(y) + '年';
            ///获取上月平均价
            Map<Id,AveragePrice__c>aPriceMap = new Map<Id,AveragePrice__c>();
            for(AveragePrice__c a :[select Id ,AveragePrice__c,Product__c from AveragePrice__c 
                                    where Product__c in : pidList and Company__c = :u.SubCompany__c and Year__c =:year and Month__c =:month]){
            	aPriceMap.put(a.Product__c,a);
            }

            Boolean noPrice = false;
            String errMsg = '';
            for (QuoteProductBean qpb : qpbList) {
                // 如果该产品没有对应的价格体系存在，提示错误信息
                if (priceMap.containsKey(qpb.productId) == false) {
                    noPrice = true;
                    // **产品（货号：*；存货编码：*）没有价格体系，请与产品经理联系
                    errMsg = qpb.name + '(' + Schema.Product__c.fields.SerialNo__c.getDescribe().getLabel() + ':' + qpb.serialNo + '; '
                             + Schema.Product__c.fields.NCCode__c.getDescribe().getLabel() + ':' + qpb.ncCode + ') '
                             + System.Label.Err_NoPrice;
                    
                    
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errMsg));
                }else{
                	Price__c p = priceMap.get(qpb.productId);
                	if(isYiqiQuote == true && p.InstCost__c == null){
                   		 noPrice = true;
                         errMsg = qpb.name +'字段 \''+ Schema.Price__c.fields.InstCost__c.getDescribe().getLabel()+ '\'为空! 请联系财务部进行维护!';
                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errMsg));
                    }

                }
            }
            // 有错就return
            if (noPrice == true) {
                //Database.rollback(sp);
                return ;
            }
             System.debug(q);
            if(isNewQuote)q.Id =null;
            upsert q;
            String accid = [select AccountId from Quote where Id = :q.Id].AccountId;

            // 报价产品delete
            List<QuoteProduct__c> delList = [select Id from QuoteProduct__c where Quote__c = :qId];
            if (delList.size() > 0) delete delList;

            // 报价产品insert
            List<QuoteProduct__c> insList = new List<QuoteProduct__c>();
            for (QuoteProductBean qpb : qpbList) {
                insList.add(
                    new QuoteProduct__c(
                        Name = qpb.name,
                        AreaDealerPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).AreaDealerPrice__c : 0,
                        InvoiceDealerPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).InvoiceDealerPrice__c : 0,
                        // HospitalSalesPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).HospitalSalesPrice__c : 0,
                        HospitalManagerPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).HospitalManagerPrice__c : 0,
                        HospitalBottomPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).HospitalBottomPrice__c : 0,
                        CompanyBottomPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).CompanyBottomPrice__c : 0,
                        LastMonthAveragePrice__c = aPriceMap.containsKey(qpb.productId) ? aPriceMap.get(qpb.productId).AveragePrice__c : 0,

                        Quote__c = q.Id,
                        Product__c = qpb.productId,
                        UnitPrice__c = qpb.marketPrice,
                        Unit__c = qpb.unit,
                        Category2__c = qpb.category2,
                        Spec__c = qpb.spec,
                        SerialNo__c = qpb.serialNo,
                        Brand__c = qpb.brand,
                        BrandShort__c = qpb.brandS,
                        Category3__c = qpb.category3,
                        Quantity__c = String.isBlank(qpb.quantity) ? 0 :Integer.valueOf(qpb.quantity),
                        Category4__c = qpb.category4,
                        Model__c = qpb.model,
                        Category1__c = qpb.category1,
                        ShortnameEng__c = qpb.sNameEng,
                        FullnameEng__c = qpb.fnameEng,
                        SalesPrice__c = String.isBlank(qpb.salesPrice) ? 0 : Decimal.valueOf(qpb.salesPrice),
                        FullnameChs__c = qpb.fNameChs,
                        Category0__c = qpb.category0,
                        RepairCost__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).RepairCost__c : 0,
                        InstCost__c =priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).InstCost__c : 0,
                        //InstCost__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).InstCost__c : 0,
                        DiscountRate__c = String.isBlank(qpb.discountRate) ? 0 : Decimal.valueOf(qpb.discountRate),
                        Account__c = accid,
                        WarrantyPeriod__c = qpb.qp.WarrantyPeriod__c,
                        ChargingStandard__c = qpb.qp.ChargingStandard__c,
                        ResponseHours__c = qpb.qp.ResponseHours__c,
                        Period__c = qpb.qp.Period__c,
                        ApplicationTraining__c = qpb.qp.ApplicationTraining__c, 
                        EquipmentCheck__c = qpb.qp.EquipmentCheck__c,
                        EquipmentCheckGoods__c = qpb.qp.EquipmentCheckGoods__c,
                        CheckPeriod__c= qpb.qp.CheckPeriod__c,
                        Consumables__c = qpb.qp.Consumables__c,
                        RapidWear__c = qpb.qp.RapidWear__c,
                        SpecialRequirement__c = qpb.qp.SpecialRequirement__c,
                        CompanyPolicyName__c  = qpb.qp.CompanyPolicyName__c,
                        WhetherImplementPolicyPrice__c = qpb.qp.WhetherImplementPolicyPrice__c,
                        CoopType__c = qpb.qp.CoopType__c,
                        Remarks__c = qpb.qp.Remarks__c ,
                        LastQuote__c = qpb.lastQuote,
                        JustPrice__c = (qpb.lastQuote == null || qpb.lastQuote == 0.0 ) ? false : ((String.isBlank(qpb.salesPrice) ? 0 : Decimal.valueOf(qpb.salesPrice)) == qpb.lastQuote ? false :true) 
                                            
                    )
                );
            }
            insert insList;
            //PageReference ref = new PageReference('/' + q.Id);
            //ref.setRedirect(true);
            isSuccess = true;
            backId = q.Id;
            //return ref;
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, System.Label.Save_OK));
        } catch (Exception e) {
            isSuccess = false;
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
        }
        //return null;
    }

    public void search() {
        String whereStr = '';
        pbList = new List<ProductBean>();
        if(!isCouldFindOverseaProduct) whereStr += '  and Overseasproducts__c != true  ';

        if (String.isBlank(name) == false) {
            whereStr += ' and Name like \'%' + name + '%\'';
        }
        if (String.isBlank(brand) == false) {
            whereStr += ' and  Brand__c like \'%' + brand + '%\'';
        }
        if (String.isBlank(serial) == false) {
            whereStr += ' and  SerialNo__c like \'%' + serial + '%\'';
        }
        if (String.isBlank(category0) == false) {
            whereStr += ' and  Category0__c = \'' + category0 + '\'';
        }
        if (String.isBlank(category1) == false) {
            whereStr += ' and  Category1__c = \'' + category1 + '\'';
        }
        if (String.isBlank(spec) == false) {
            whereStr += ' and  Spec__c like \'%' + spec + '\'%';
        }
        if (String.isBlank(model) == false) {
            whereStr += ' and  Model__c like \'%' + model + '\'%';
        }
        if (String.isBlank(productType) == false) {
            whereStr += ' and  TypeName__c like \'%' + productType + '\'%';
        }

        
        
         whereStr += ' and Purpose__c = \'' + purpose + '\'';
       
        
        List<Product__c> plist = Database.query(this.soqlStr() + whereStr + ' limit 500');
        // 获取价格信息
        Set<Id> pidList = new Set<Id>();
        for (Product__c p : plist) {
            pidList.add(p.Id);
        }
     
        Map<Id, Price__c> priceMap = getPriceMap(pIdList);

        for (Product__c p : plist) {
            pbList.add(new ProductBean(p, priceMap.get(p.Id), opp));
        }
    }
    ///获取价格
     private Map<Id,Price__c> getPriceMap (Set<Id> pidList){

         Map<Id, Price__c> priceMap = new Map<Id, Price__c>();
         for (Price__c pr : [select Id, Product__c, MarketPrice__c, AreaDealerPrice__c, InvoiceDealerPrice__c, RepairCost__c, InstCost__c,HospitalManagerPrice__c
                                    ,HospitalBottomPrice__c,CompanyBottomPrice__c,TaxRate__c from Price__c 
                                     where Product__c in : pidList and Company__c = :u.SubCompany__c and ClosePrice__c != true ]) {
                    priceMap.put(pr.Product__c, pr);
        }

        return priceMap;


    }
    public void exchange() {
        // 去掉已勾选的报价产品
        Set<Id> existPid = new Set<Id>();
        List<QuoteProductBean> stayList = new List<QuoteProductBean>();
        for (QuoteProductBean qpb : qpbList) {
            if (qpb.chk == false) {
                stayList.add(qpb);
            }
        }
        qpbList.clear();
        qpbList.addAll(stayList);

        // 收集已选报价产品的产品Id
        for (QuoteProductBean qpb : qpbList) {
            existPid.add(qpb.productId);
        }
        for (QuoteProductBean qpb : inList) {
            if(qpb.chk == true && !existPid.contains(qpb.productId)){
               existPid.add(qpb.productId);
               qpb.chk = false;
                qpbList.add(qpb.clone());
            }
            
        }

        // 加入已勾选的产品
        for (ProductBean p : pbList) {
            if (p.chk == true && existPid.contains(p.rec.Id) == false) {
                qpbList.add(new QuoteProductBean(p.rec, p.pr));
                p.chk = false;
            }
        }
        tableHeight = qpbList.size() < 10 ? 180 + qpbList.size()*32 : 400;

    }
	
    private String soqlStr() {
        Schema.DescribeSobjectResult m = Product__c.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
        
        String sql = 'select ';
        String fields = '';
        for (String field : fieldMap.keySet()) {
            if (fields.length() > 0) {
                fields += ', '; 
            }
            fields += field;
        }
        sql += fields;
        sql += ' from Product__c where Name != null and RecordType.DeveloperName =\'NCProduct\' ';

        return sql;
    }

    public class QuoteProductBean {
        public QuoteProduct__c rec { get; set; }
        public String name { get; set; }
        public String serialNo { get; set; }
        public String ncCode { get; set; }
        public String spec { get; set; }
        public String brand { get; set; }
        public String brandS { get; set; }
        public String unit { get; set; }
        public String model { get; set; }
        public String sNameEng { get; set; }
        public String fnameEng { get; set; }
        public String fNameChs { get; set; }
        public String quantity { get; set; }
        public String category0 { get; set; }
        public String category1 { get; set; }
        public String category2 { get; set; }
        public String category3 { get; set; }
        public String category4 { get; set; }
        public Decimal marketPrice { get; set; }
        public String discountRate { get; set; }
        public String salesPrice { get; set; }
        public Decimal swjhdj {get;set;}
        // public Decimal repairCost { get; set; }
        // public Decimal instCost { get; set; }
        public String comment { get; set; }
        // public String low { get; set; }
        public Id productId { get; set; }
        public Boolean chk { get; set; }
        public Decimal lastQuote {get; set;}//上次报价
        
        public QuoteProduct__c qp {get;set;}//报价产品

    
        
        
        
        public QuoteProductBean(QuoteProduct__c record,Boolean isLastquote,Price__c pr) {
            rec = record;
            name = record.Name;
            serialNo = record.SerialNo__c;
            ncCode = record.Product__r.NCCode__c;
            spec = record.Spec__c;
            brand = record.Brand__c;
            brandS = record.BrandShort__c;
            unit = record.Unit__c;
            model = record.Model__c;
            sNameEng = record.ShortnameEng__c;
            fnameEng = record.FullnameEng__c;
            fNameChs = record.FullnameChs__c;
            quantity = String.valueOf(record.Quantity__c);
            category0 = record.Category0__c;
            category1 = record.Category1__c;
            category2 = record.Category2__c;
            category3 = record.Category3__c;

            marketPrice = pr != null ? pr.MarketPrice__c : 0;
            discountRate = String.valueOf(record.DiscountRate__c);
            salesPrice = String.valueOf(record.SalesPrice__c);
            swjhdj = record.Product__r.swjhdj__c;

            
            comment = record.Comment__c;
            productId = record.Product__c;
            chk = false;
            lastQuote = isLastquote  ? record.SalesPrice__c : record.LastQuote__c;
            //合同条款
            qp = record;
        }

        public QuoteProductBean(IntentProduct__c record,Price__c pr) {
            rec = null;
            name = record.Name;
            serialNo = record.SerialNo__c;
            ncCode = record.Product__r.NCCode__c;
            spec = record.Spec__c;
            brand = record.Brand__c;
            brandS = null;
            unit = record.Unit__c;
            model = record.Model__c;
            sNameEng = null;
            fnameEng = null;
            fNameChs = null;
           // quantity = String.valueOf(record.Quantity__c);
            category0 = record.NewCategory0__c;
            category1 = record.NewCategory1__c;
            category2 = record.NewCategory2__c;
            category3 = record.NewCategory3__c;
            // category4 = record.Category4__c;
            marketPrice = pr != null ? pr.MarketPrice__c : 0; 
            discountRate = '0';
            salesPrice = String.valueOf(record.SalesPrice__c);
            swjhdj = record.Product__r.swjhdj__c;
            // repairCost = record.RepairCost__c;
            // instCost = record.InstCost__c;
            comment = null;
            // low = record.LowerThanAreaDealderPrice__c == true ? '*' : '';
            productId = record.Product__c;
            chk = false;
            qp = new QuoteProduct__c();
            lastQuote = null;
            qp = new QuoteProduct__c ();

        }

        public QuoteProductBean(Product__c record, Price__c price) {
            rec = null;
            name = record.Name;
            serialNo = record.SerialNo__c;
            ncCode = record.NCCode__c;
            spec = record.Spec__c;
            brand = record.Brand__c;
            brandS = record.BrandShort__c;
            unit = record.Unit__c;
            model = record.Model__c;
            sNameEng = record.ShortnameEng__c;
            fnameEng = record.FullnameEng__c;
            fNameChs = record.FullnameChs__c;
            //quantity = '1';
            category0 = record.Category0__c;
            category1 = record.Category1__c;
            category2 = record.Category2__c;
            category3 = record.Category3__c;
            swjhdj = record.swjhdj__c;

           
            marketPrice = price != null ? price.MarketPrice__c : 0; 
            discountRate = '0';
            comment = null;
            // low = '';
            productId = record.Id;
            chk = false;
            qp = new QuoteProduct__c();
            lastQuote = null;
        }
    }

    public class ProductBean {
        public Product__c rec { get; set; }
        public Price__c pr { get; set; }
        public Decimal marketPrice { get; set; }
        public Boolean chk { get; set; }

        public ProductBean(Product__c record, Price__c price, Opportunity opp) {
            rec = record;
            pr = price;
            if (price != null) {
                // 终端客户业务机会的报价显示产品的售价，不然显示区域经销商价
                if (opp.RecordType.Name == System.Label.Rt_ShowMarketPriceOpp) {
                    marketPrice = price.MarketPrice__c;
                } else {
                    marketPrice = price.AreaDealerPrice__c;
                }
            } else {
                marketPrice = 0;
            }
            chk = false;
        }
    }
}